package com.ysh.sevlet;

import com.ysh.domain.Login;
import com.ysh.service.LoginService;
import com.ysh.service.impl.LoginServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 测试
        //    resp.getWriter().write("跳转成功！");
        // 获取页面参数
        String username = req.getParameter("user");
        String pwd = req.getParameter("password");

        // 调用service层
        LoginService loginService = new LoginServiceImpl();
        Login login = loginService.findByNameAndPwd(username, pwd);
        if (login != null){
            req.getRequestDispatcher("/user?method=findAll").forward(req,resp);
        }else{
            req.setAttribute("error","登录错误！");
            req.setAttribute("username",username);
            req.getRequestDispatcher("/pages/login.jsp").forward(req,resp);
        }


    }
}
